//
//  HomeCycleImageCell.m
//  CenturyCity
//
//  Created by beisheng03 on 2017/11/22.
//  Copyright © 2017年 beisheng. All rights reserved.
//

#import "HomeCycleImageCell.h"
#import <SDCycleScrollView/SDCycleScrollView.h>
#import "RentHouseDetailVC.h"
#import "GoodDetailController.h"
#import "EmploymentDetailVC.h"

#import "AboutusController.h"
@interface HomeCycleImageCell ()<SDCycleScrollViewDelegate>
@property (nonatomic, weak) SDCycleScrollView *cycleView;
@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomMargin;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bgViewH;

@end

@implementation HomeCycleImageCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.bottomMargin.constant = 0;
    
    self.bgViewH.constant = KScreenW / 8.0 * 3;
    
    [self setupTopScrollview];
    // Initialization code
}


- (void)setupTopScrollview{
    
    if (!_cycleView) {
        // 网络加载 --- 创建自定义图片的pageControlDot的图片轮播器
        SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, KScreenW, KScreenW / 8.0 * 3) delegate:self placeholderImage:[UIImage imageNamed:@"placeholder"]];
        
        cycleScrollView.autoScrollTimeInterval = 3;
        cycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
        cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
        //    cycleScrollView3.currentPageDotImage = [UIImage imageNamed:@"pageControlCurrentDot"];
        //    cycleScrollView3.pageDotImage = [UIImage imageNamed:@"pageControlDot"];
        _cycleView = cycleScrollView;
        
        
        [self.bgView addSubview:_cycleView];
        
        
    }
    
    
}

- (void)setUrls:(NSArray *)urls{
    _urls = urls;
    
    self.cycleView.imageURLStringsGroup = _urls;
    
}


- (void)setModels:(NSArray<HomeSubModel *> *)models{
    _models = models;
    
     UIImage *image0 = [[[SDWebImageManager sharedManager] imageCache] imageFromDiskCacheForKey:_models.firstObject.adv_image];
    
    NSLog(@"%f",image0.size.width);
    NSLog(@"%f",image0.size.height);

     self.bgViewH.constant = KScreenW / 64.0 * 21.0;
     self.cycleView.height = KScreenW / 64.0 * 21.0;
    
     self.cycleView.imageURLStringsGroup = [_models valueForKeyPath:@"adv_image"];


    if (image0) {
        
        CGFloat imageViewH = KScreenW / image0.size.width * image0.size.height;
        // 重置放大缩小的比例
        self.bgViewH.constant = imageViewH;

        self.cycleView.height = imageViewH;
        
    } else {
        
        MJWeakSelf;
        [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:_models.firstObject.adv_image] options:kNilOptions progress:^(NSInteger receivedSize, NSInteger expectedSize) {
            
        } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
            
            if (image) {
                
                CGFloat imageViewH2 = KScreenW / image.size.width * image.size.height;
                // 重置放大缩小的比例
                weakSelf.bgViewH.constant = imageViewH2;
                
                weakSelf.cycleView.height = imageViewH2;
                
            }
            
            
        }];
    }
    
    
    
}



#pragma mark - SDCycleScrollViewDelegate

/** 点击图片回调 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
    

    
    HomeSubModel *subModel = self.models[index];
    
    if ([subModel.class_id isEqualToString:@"1"]) {
        
        RentHouseDetailVC *temp = [[RentHouseDetailVC alloc]init];
        
        temp.houseId = subModel.ID;
        
        [self.viewController.navigationController pushViewController:temp animated:YES];
        
        
    } else if ([subModel.class_id isEqualToString:@"2"]){
        
        EmploymentDetailVC *tempVC = [[EmploymentDetailVC alloc]init];
        
        tempVC.IdStr = subModel.ID;
        
        [self.viewController.navigationController pushViewController:tempVC animated:YES];
        
    }else if ([subModel.class_id isEqualToString:@"3"]){
        
        GoodDetailController *tempVC = [[GoodDetailController alloc]init];
        
        OneGoodsModel *tempModel = [[OneGoodsModel alloc]init];
        
        tempModel.goods_id = subModel.ID;
        
        tempModel.goods_image_url = subModel.adv_image;
        
        tempVC.goodsModel = tempModel;
        
        [self.viewController.navigationController pushViewController:tempVC animated:YES];
        
        
    }else{
        
        AboutusController *temp = [[AboutusController alloc]init];
        
        temp.urlString = subModel.content;
        
        [self.viewController.navigationController pushViewController:temp animated:YES];

    }
    
    
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
